package com.springboot.wisdom.user.controller;

import cn.dev33.satoken.stp.StpUtil;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.github.xiaoymin.knife4j.annotations.ApiSupport;
import com.springboot.wisdom.common.common.bean.DeleteDTO;
import com.springboot.wisdom.common.common.bean.MyResult;
import com.springboot.wisdom.common.common.bean.PageQueryDTO;
import com.springboot.wisdom.serviceApi.email.model.vo.UpdateEmailVO;
import com.springboot.wisdom.serviceApi.user.model.vo.UpdatePasswordVO;
import com.springboot.wisdom.serviceApi.user.model.vo.UserVO;
import com.springboot.wisdom.user.service.UserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;

/**
 * @Description
 * @Author LY L
 * @Date 2024-11-03
 */
@Api(tags = "用户管理模块【仅管理员可操作】")
@ApiSupport(author = "LY L")
@RestController
@RequestMapping(value = "/user", produces = {"application/json;charset=UTF-8"})
@Slf4j
@Validated     // springboot 2.5.* 版本需要在类上标注 @Validated 注解校验注解才会生效。
public class UserController {

    @Autowired
    private UserService userService;


    /******************************************** crud操作  ***********************************************************/
    /********************************************   add   ***********************************************************/
    @ApiOperation(value = "添加用户", notes = "不需设置的值：id，registerDate，role")
    @PostMapping("/add/info")
    public MyResult<String> addInfo(@RequestBody @Validated UserVO userVO) {
        // todo 添加用户。
        return userService.addInfo(userVO);
    }

    /******************************************** delete  ***********************************************************/
    @ApiOperation(value = "删除用户", notes = "")
    @DeleteMapping("/del/info/{userId}")
    public MyResult<String> delInfo(@PathVariable String userId) {
        return userService.delInfo(userId);
    }

    @ApiOperation(value = "批量删除用户", notes = "")
    @DeleteMapping("/del/info/list")
    public MyResult<String> delInfo(@RequestBody @Validated DeleteDTO<String> deleteIds) {
        return userService.delInfo(deleteIds);
    }


    /******************************************** update  ***********************************************************/

    @ApiOperation(value = "修改用户信息", notes = "")
    @PostMapping("/change/info")
    public MyResult<String> changeInfo(@RequestBody @Validated UserVO userVO) {
        return userService.changeInfo(userVO);
    }

    /******************************************** get  ***********************************************************/
    @ApiOperation(value = "用户列表分页", notes = "")
    @GetMapping("/admin/userInfo/page")
    public MyResult<Page<UserVO>> getInfoPageBy(PageQueryDTO pageQueryDTO, UserVO userVO) {
        return userService.getInfoPageBy(pageQueryDTO, userVO);
    }

    @ApiOperation(value = "查询用户信息", notes = "")
    @GetMapping("/user/userInfo/{userId}")
    public MyResult<UserVO> getUserById(@PathVariable String userId) {
        return userService.getUserById(userId);
    }


    /******************************************** 管理员操作  ***********************************************************/
    @ApiOperation(value = "重置密码", notes = "")
    @PutMapping("/admin/userInfo/rePassword/{id}")
    public MyResult<String> rePassword(@PathVariable String id, HttpServletRequest request) {
        return userService.rePassword(id, request);
    }

    @ApiOperation(value = "账号状态更改", notes = "")
    @PutMapping("/admin/userInfo/setStatus/{id}")
    public MyResult<String> setStatus(@PathVariable String id, HttpServletRequest request) {
        // TODO 禁用账号。
        return userService.setStatus(id, request);
    }

    @ApiOperation(value = "注销账号", notes = "")
    @DeleteMapping("/admin/userInfo/delete/{id}")
    public MyResult<String> deleteInfo(@PathVariable String id, HttpServletRequest request) {
        return userService.deleteInfo(id, request);
    }


    /******************************************** 个人中心  ***********************************************************/

    @ApiOperation(value = "修改密码", notes = "")
    @PostMapping("/user/change/password")
    public MyResult<String> changePassword(@RequestBody @Validated UpdatePasswordVO updatePasswordVO) {
        return userService.changePassword(StpUtil.getLoginId().toString().split("_")[1]
                , updatePasswordVO.getOldPassword()
                , updatePasswordVO.getNewPassword());
    }

    @ApiOperation(value = "修改姓名", notes = "只需要字段 name")
    @PostMapping("/user/userInfo/change/name")
    public MyResult<String> changeName(@RequestBody UserVO userVO) {
        return userService.changeName(StpUtil.getLoginId().toString().split("_")[1], userVO.getUsername());
    }

    @ApiOperation(value = "修改手机号", notes = "只需要字段 phone")
    @PostMapping("/user/userInfo/change/phone")
    public MyResult<String> changePhone(@RequestBody UserVO userVO) {
        return userService.changePhone(StpUtil.getLoginId().toString().split("_")[1], userVO.getPhone());
    }

    @ApiOperation(value = "修改头像", notes = "只需要字段 avatarUrl")
    @PostMapping("/user/userInfo/change/avatarUrl")
    public MyResult<String> changeAvatarUrl(@RequestBody UserVO userVO) {
        return userService.changeAvatarUrl(StpUtil.getLoginId().toString().split("_")[1], userVO.getAvatarUrl());
    }

    /**
     * 修改邮箱。
     */
    @ApiOperation(value = "修改邮箱", notes = "")
    @PostMapping("/edit/email")
    public MyResult<String> editEmail(@RequestBody @Validated UpdateEmailVO updateEmailVO) {
        return userService.editEmail(updateEmailVO.getOldEmail(), updateEmailVO.getOldCode()
                , updateEmailVO.getNewEmail(), updateEmailVO.getNewCode());
    }

    /********************************************   get   ***********************************************************/

    @ApiOperation(value = "查询用户信息【根据用户名】", notes = "")
    @GetMapping("/get/username/{username}")
    public MyResult<UserVO> getUserByUsername(@PathVariable String username) {
        return userService.getUserByUsername(username);
    }

    // 编辑信息。
}
